package org.example.carplatoon.config;

import lombok.extern.slf4j.Slf4j;
import org.example.carplatoon.Interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;
    //页面跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toIndexPage").setViewName("/index");
        registry.addViewController("/").setViewName("/index");
    }

    @Value("${car.path}")
    private String basePath;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginCheckInterceptor)
                .addPathPatterns("/**")//添加拦截器
                .excludePathPatterns(//排除某些url
                        "/login",
                        "/register",
                        "/getCode",
                        "/userLogin",
                        "/**/*.html",
                        "/**/*.js",
                        "/**/*.css",
                        "/images/**"

                );
    }

    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始静态资源映射");
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/images/**").addResourceLocations("file:" + basePath + "images/");
    }
}